package com.woniuxy.oa.web;

import com.woniuxy.oa.entity.Message;
import com.woniuxy.oa.entity.Notice;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.service.MessageService;
import com.woniuxy.oa.service.NoticeService;
import com.woniuxy.oa.util.OaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("notice")
@RestController
@CrossOrigin("*")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 公告列表
     *
     * @param notice
     * @return
     */
    @RequestMapping("list")
    public ResponseVO list(@RequestBody(required = false) Notice notice) {
        return new ResponseVO("200", "查询成功", noticeService.getPageBean(notice, notice.getPage()));
    }

    /**
     * 删除公告
     *
     * @param ids
     * @return
     */
    @GetMapping("del/{ids}")
    public ResponseVO del(@PathVariable String[] ids) {
        noticeService.del(ids);
        return new ResponseVO("200", "删除成功", null);
    }


    /**
     * 添加公告
     *
     * @param notice
     * @return
     */
    @PostMapping("add")
    public ResponseVO add(Notice notice, @RequestParam("file") MultipartFile[] files, @RequestParam("empid") String empid) {
     notice.setPubId(empid);
        List<String> imges = new ArrayList<>();
        String filePath = "D://wnshop_imges//";
        if (files != null) {
            try {
                for (MultipartFile file : files) {
                    String path = "goods/" + OaUtil.getId();
                    file.transferTo(new File(filePath + path));
                    imges.add(path);
                }
            } catch (Exception e) {
            }
        }
        System.out.println("imges: " + imges.size());
        noticeService.add(notice, imges);
        return new ResponseVO("200", "商品添加成功", null);
    }
}
